/*
Copyright (c) 2014, Rick Wilson
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
    * Redistributions of source code must retain the above copyright
      notice, this list of conditions and the following disclaimer.
    * Redistributions in binary form must reproduce the above copyright
      notice, this list of conditions and the following disclaimer in the
      documentation and/or other materials provided with the distribution.
    * Neither the name of 'Taareekah' nor the
      names of its contributors may be used to endorse or promote products
      derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
import world.GenericTerrainMap;
import worldgeneration.WorldGenerator;
import world.Terrain;
import world.BiomeType;
import world.BiomeMap;
import util.BMPFile;
import util.BitmapPixelShader;
import util.Point;

import java.util.Map;
import java.util.HashMap;
import java.util.List;
import java.util.ArrayList;

def getAllValidBiomes(int x, int y, Terrain terrain, Map<String, BiomeType> biomes) {
    Map<String, Float> values = terrain.getAllMapValuesAtPoint(x, y);
    List<BiomeType> valid_biomes = new ArrayList<BiomeType>();
    valid_biomes.addAll(biomes.values());
    valid_biomes.remove(biomes.get("null"));
    valid_biomes.remove(biomes.get("ocean"));
    valid_biomes.remove(biomes.get("coast"));
    valid_biomes.remove(biomes.get("mountain"));
    for (Map.Entry<String, Float> values_entry : values.entrySet()) {
        for (Map.Entry<String, BiomeType> biomes_entry : biomes.entrySet()) {
            if (biomes_entry.getKey() == "null") {
                continue;
            }
            if (!valid_biomes.contains(biomes_entry.getValue())) {
                continue;
            }
            if (values_entry.getKey() == "temperature") {
                float value = values_entry.getValue() + 40.0f;
                
                if (!biomes_entry.getValue().doesValueMeetCritera(values_entry.getKey(), value)) {
                    valid_biomes.remove(biomes_entry.getValue());
                }
            } else {
                if (!biomes_entry.getValue().doesValueMeetCritera(values_entry.getKey(), values_entry.getValue())) {
                    valid_biomes.remove(biomes_entry.getValue());
                }
                
            }
        }
    }
    
    return valid_biomes;
}

def doesPointTouchOcean(Point p, Terrain terrain, int size) {
    Vector<Point> neighbors = p.getNeighborsWithinRange(0, size);
    for (int i = 0; i < neighbors.size(); i++) {
        if (terrain.getValueOfMapAtPoint("elevation", neighbors.get(i)) == 0.0f) {
            return true;
        }
    }
    
    return false;
}

def markBiomes(Terrain terrain, Map<String, BiomeType> biomes_types, int size, BiomeMap biome_map, long seed) {
    BiomeType default_type = biomes_types.get("null");
    Random rand = new Random(seed);
    for (int x = 0; x < size; x++) {
        for (int y = 0; y < size; y++) {
            Point p = new Point(x, y);
            if (terrain.getValueOfMapAtPoint("elevation", p) != 0.0f && doesPointTouchOcean(p, terrain, size)) {
                biome_map.setBiomeAtPoint(x, y, biomes_types.get("coast"));
                continue;
            } else if (terrain.getValueOfMapAtPoint("elevation", p) == 0.0f) {
                biome_map.setBiomeAtPoint(x, y, biomes_types.get("ocean"));
                continue;                
            } else if (terrain.getValueOfMapAtPoint("elevation", p) >= 190.0f) {
                biome_map.setBiomeAtPoint(x, y, biomes_types.get("mountain"));
                continue;                
                
            }
            List<BiomeType> valid_biomes = getAllValidBiomes(x, y, terrain, biomes_types);
            if (valid_biomes.size() == 0) {
                biome_map.setBiomeAtPoint(x, y, default_type);
            } else {
                int index = rand.nextInt(valid_biomes.size());
                biome_map.setBiomeAtPoint(x, y, valid_biomes.get(index));
            }
        }
    }
}

class Shader extends BitmapPixelShader {
    private BiomeMap biome_map;
    private Map<BiomeType, int[]> colors;
    
public void createColorsForBiomes(Map<String, BiomeType> biomes_types) {
    colors = new HashMap<BiomeType, int[]>();
    //Blue for ocean, Grey for mountain, Bright Green for grassland, tan for desert, Dark Green for forest, purple for swamp

    
    colors.put(biomes_types.get("null"), [0, 0, 0]);
    colors.put(biomes_types.get("swamp"), [170, 0, 255]);
    colors.put(biomes_types.get("ocean"), [0, 0, 255]);
    colors.put(biomes_types.get("sand desert"), [255, 0, 0]);
    colors.put(biomes_types.get("coast"), [210, 180, 140]);
    colors.put(biomes_types.get("forest"), [0, 100, 0]);
    colors.put(biomes_types.get("grassland"), [144, 238, 144]);
    colors.put(biomes_types.get("mountain"), [105, 105, 105]);

}
    Shader(BiomeMap biome_map, Map<String, BiomeType> biomes_types) {
        this.biome_map = biome_map;
        createColorsForBiomes (biomes_types);
    }
    
    public int[] getColorAtPoint(int x, int y) {
        return colors.get(biome_map.getBiomeAtPoint(x, y));
    }
}
Terrain terrain = worldgenerator.getTerrain();
BiomeMap biome_map = terrain.getBiomeMap();
Map<String, BiomeType> biomes_types = biome_map.getBiomeTypes();
int size = worldgenerator.getWorldSize();
long seed = worldgenerator.getSeed();
markBiomes(terrain, biomes_types, size, biome_map, seed);
Shader shader = new Shader(biome_map, biomes_types);
//biome_map.setShader(shader);
//BMPFile.saveBMP("biomes.bmp", size, size, shader);
